package com.lanou3g.dllo.baidumusic.download;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.database.MyFavorite;
import com.lanou3g.dllo.baidumusic.database.SingleLiteOrm;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlaymusicBean;
import com.lanou3g.dllo.baidumusic.main.playmusic.SongPlayBean;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.net.URL;

/**
 * Created by dllo on 16/7/7.
 */
public class SingleDownload {

    private static SingleDownload singleDownload;
    private DownloadManager downloadManager;

    private SingleDownload() {
        downloadManager = (DownloadManager) MyApp.context.getSystemService(Context.DOWNLOAD_SERVICE);
    }

    public static SingleDownload getSingleDownload() {
        if (singleDownload == null) {
            synchronized (SingleDownload.class) {
                if (singleDownload == null) {
                    singleDownload = new SingleDownload();
                }
            }
        }
        return singleDownload;
    }

    public void Download(String songId) {
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu." +
                "ting.song.play&format=json&callback=&songid=" + songId + "&_=1413017198449";
        MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<SongPlayBean>() {
            @Override
            public void getGsonListener(SongPlayBean data) {
                String url = data.getBitrate().getFile_link();
                String songId = data.getSonginfo().getSong_id();
                String title = data.getSonginfo().getTitle();
                QueryBuilder<DownloadBean> queryBuilder = new QueryBuilder<>(DownloadBean.class);
                queryBuilder.whereEquals("title",title);
                if (SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(queryBuilder).size() != 0) {
                    Toast.makeText(MyApp.context, "已经下载了,无需重复下载", Toast.LENGTH_SHORT).show();
                } else {
                    Uri resource = Uri.parse(url);
                    DownloadManager.Request request = new DownloadManager.Request(resource);
                    // 表示下载允许的网络类型
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
                    // 阻止手机在漫游状态下下载
                    request.setAllowedOverRoaming(false);
                    // 设置文件类型
                    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
                    String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
                    request.setMimeType(mimeString);
                    // 再通知栏显示
                    request.setShowRunningNotification(true);
                    request.setVisibleInDownloadsUi(true);
                    // SD卡 的目录下的download文件夹
                    request.setDestinationInExternalPublicDir("/music/mp3",title + ".mp3");
                    long id = downloadManager.enqueue(request);
                    SingleLiteOrm.getSingleLiteOrm().getLiteOrm().insert(new DownloadBean(title,songId));
                }
            }

        },SongPlayBean.class);

    }
}
